import {storeBindingsBehavior} from "mobx-miniprogram-bindings";
import mall from "@src/store/mall";
import base from "@src/store/base";
import router from "@src/router";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: mall,
    fields: ['changeDeliveryType','deliveryType','chooseAddress']
  }, {
    store: base,
    fields: ['isiPhoneX', 'themeColor', 'themeTitleColor', 'barHeight']
  }],
  properties: {
    deliveryType:{
      type:String,
      value:"",
    },
    delivery: {
      type: Object,
      value: null
    },
    defaultAddress: {
      type: Object,
      value: null
    },
    arrivalTime: {
      type: String,
    },
    prepareTips: {
      type: String,
    },
    goodsPresell: {
      type: Object,
      value:{}
    },
    presell: {
      type: Boolean,
      value: false
    },
    groupInfo:{
      type:Object,
      value:null
    },

  },
  data: {
    address: null,
    selectIndex: 0,
    leaderCollect:false
  },
  observers: {
    'chooseAddress,defaultAddress': function (chooseAddress, defaultAddress) {
      // debugger
      if (chooseAddress) {
        this.setAddress(chooseAddress)
        return
      }
      if (defaultAddress) {
        this.setAddress(defaultAddress)
      }
      if(!chooseAddress&&!defaultAddress){
        this.setAddress();
      }
    },
    'changeDeliveryType':function (changeDeliveryType){
        if(changeDeliveryType){
          this.setData({deliveryType:changeDeliveryType})
        }
    },
    'delivery,arrivalTime': function (delivery, arrivalTime) {
      if (!delivery) {
        return;
      }
      if (delivery.limit === 2) {
        this.setData({selectIndex: 1})
        return;
      }
      this.setData({selectIndex: arrivalTime ? 1 : 0})
    }
  },
  methods: {
    handleLeaderCollect(e){
      this.setData({leaderCollect:e.detail})
      this.triggerEvent("handleLeaderCollect",e.detail)
    },
    handleToChoose() {
      router.push(
          {
            'name': 'mall/service-stores',
            params: {deliveryType: 'LOCAL', activeTab: 'address'},
            events: {
              'changeStore': (data) => {
                // 这里只是修改店铺
                this.triggerEvent("store", data)
              },
              'changeAddress': (data) => {
                console.info("changeAddress", data)
                // 这里切换了配送方式和地址
                this.setAddress(data.address);
              }
            }
          });
      // router.push({
      //   name: "mall/address"
      // })
    },
    handleArrivalTime() {
      this.triggerEvent("arrivalTime")
    },
    handleNoTime() {
      if (this.data.delivery.limit === 2) {
        wx.showToast({title: '暂不支持立即送出, 请预约送达', icon: 'none'})
        return;
      }
      this.triggerEvent("arrivalTime", "reset")
    },
    setAddress(address=null) {
      // 发送更新地址信息
      this.triggerEvent('address', address);
      this.setData({address: address});
    },
    handleReservedPhone(e){
      this.triggerEvent('handleReservedPhone',e.detail)
    },
    handleSwitchType(e) {
      router.push(
          {
            'name': 'mall/service-stores',
            params: {deliveryType: 'SELF_PICKUP', showTabs: true, activeTab: 'store'},
            events: {
              'changeStore': (data) => {
                // 这里只是修改店铺
                this.triggerEvent("store", data)
              },
              'changeAddress': (data) => {
                console.info("changeAddress", data)
                // 这里切换了配送方式和地址
                this.setAddress(data.address);
              }
            }
          });
    },
  }
});
